package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.entity.Photo;
import com.hadwinling.blog.service.PhotoService;
import com.hadwinling.blog.util.FileUpload;
import com.hadwinling.blog.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/19 14:43
 */
@Controller
public class UserPhotoController {

    @Autowired
    private PhotoService photoService;
    @Autowired
    private FileUpload fileUpload;

    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;


    /**
     * 跳转到查看美图页面
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/user/photo")
    public String toPhotoPage(Model model, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,HttpServletRequest request){
        Admin admin = (Admin) request.getSession().getAttribute("currUser");
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Photo> photoList = photoService.selectAllByAdminidAndState(admin.getId(),1);
        PageInfo<Photo> pageInfo = new PageInfo<>(photoList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("photoList",photoList);
        return "UserCenter/photo.html";
    }


    /**
     * 跳转到发布美图页面
     * @return
     */
    @GetMapping("/user/addPhoto")
    public String toAddPhotoPage(){
        return "UserCenter/photoAdd";
    }

    /**
     * 执行发布操作
     * @param imgFile
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/user/addPhoto")
    @ResponseBody
    public Result  addPhoto(MultipartFile imgFile, HttpServletRequest request) throws IOException {
//        System.out.println("上传头像");
        String filepath = fileUpload.FileUploadToQiNiu(imgFile);
//        System.out.println("filepath:"+filepath);
        Admin admin = (Admin) request.getSession().getAttribute("currUser");
        Photo photo = new Photo();
        photo.setPhoto(filepath);
        photo.setState(1);
        photo.setCreatetime(new Date());
        photo.setAdminid(admin.getId());

        photoService.insertSelective(photo);

        return  new Result(200,filepath);
    }

    @PostMapping("/user/deletePhoto")
    @ResponseBody
    public String deletePhoto(@RequestParam(name = "photoid") int photoid){
        Photo photo = photoService.selectByPrimaryKey(photoid);
        photo.setState(0);
        photoService.updateByPrimaryKeySelective(photo);
        return "1";
    }


}
